/**
 * Created by Administrator on 2015/12/29.
 */
angular.module('appItfAop.service',[])
  .service('appItfAopService',['$http','$q','$ionicLoading','$localstorage',function($http,$q,$ionicLoading,$localstorage) {


      //短信验证码获取
      var generateSmsVerifyCode_resp={};
      //登录
      var login_resp = {};

      //获取信息
      var Nav_Info={};
      //获取信息
      var User_Info = {};
      //获取信息
      var notifi_Info = {};
      //获取信息
      var tasklist = {};
      //获取信息
      var custlist = {};
      //版本更新
      var queryLatestVersion_resp = {};
      return {

          //短信验证码获取
          get_generateSmsVerifyCode_resp:function()
          {
              return generateSmsVerifyCode_resp;
          },
          //登录操作
          do_Login_req: function (params) {
              var deferred = $q.defer();
              var obj = get_Login_Data(params);
              var strUrl = "AppAPI/login";
              postData(strUrl, obj, login_resp, deferred);
              return deferred.promise;
          },
          //短信验证码获取
          do_generateSmsVerifyCode_req:function(params)
          {
              var deferred = $q.defer();
              var obj=get_Login_Data(params);
              var strUrl = "AppAPI/generateSmsVerifyCode";
              postData(strUrl, obj, generateSmsVerifyCode_resp, deferred);
              return deferred.promise;
          },
          //获取菜单信息
          do_get_NavInfo_req:function(typeid){
              var deferred = $q.defer();
              var obj = {};
              var strUrl = "AppAPI/navInfo/"+typeid;
              postData(strUrl, obj, Nav_Info, deferred);
              return deferred.promise;
          },
          //获取菜单信息
          do_get_twoLayerNavInfo_req: function (params) {
              var deferred = $q.defer(); 
              var strUrl = "AppAPI/twoLayerNavInfo/" + params.Type_ID;
              postData(strUrl, params, Nav_Info, deferred);
              return deferred.promise;
          },
          //获取用户信息
          do_get_UserInfo_req:function(params){
              var deferred = $q.defer();
              var strUrl = "AppAPI/userInfoList";
              postData(strUrl, params, User_Info, deferred);
              return deferred.promise;
          },
          //获取用户信息
          do_get_DomanInfo_req:function(typeid){
              var deferred = $q.defer();
              var strUrl = "AppAPI/navInfo/"+typeid;
              postData(strUrl, {}, Nav_Info, deferred);
              return deferred.promise;
          },
          //获取新闻列表
          do_get_newslist_req:function(typeid){
              var deferred = $q.defer();
              var strUrl = "AppAPI/newslist/"+typeid;
              postData(strUrl, {}, Nav_Info, deferred);
              return deferred.promise;
          },
          //获取新闻
          do_get_newsview_req:function( id){
              var deferred = $q.defer();
              var strUrl = "AppAPI/newsview/"+ id;
              postData(strUrl, {}, Nav_Info, deferred);
              return deferred.promise;
          },
          //获取通知公告
          do_get_notificationlist_req: function () {
              var deferred = $q.defer();
              var strUrl = "AppAPI/notificatonlist/";
              postData(strUrl, {}, notifi_Info, deferred);
              return deferred.promise;
          },
          //获取通知公告
          do_get_notification_req: function (id) {
              var deferred = $q.defer();
              var strUrl = "AppAPI/notificaton/" + id;
              postData(strUrl, {}, notifi_Info, deferred);
              return deferred.promise;
          }, 
          //获取任务
          do_get_tasklist_req: function (params) {
              var deferred = $q.defer();
              var strUrl = "AppAPI/tasklist/"  ;
              postData(strUrl, params, tasklist, deferred);
              return deferred.promise;
          },
          //获取任务客户明细
          do_get_taskcustlist_req: function (params) {
              var deferred = $q.defer();
              var strUrl = "AppAPI/taskcustlist/" + params.Task_ID.trim() + '/';
              postData(strUrl, params, custlist, deferred);
              return deferred.promise;
          },
          //获取任务客户明细
          do_get_custcustlist_req: function (params) {
              var deferred = $q.defer();
              var strUrl = "AppAPI/custcustlist/";
              postData(strUrl, params, custlist, deferred);
              return deferred.promise;
          },
          //获取个人中心信息
          do_get_centerInfo_req: function (params) {
              var deferred = $q.defer();
              var strUrl = "AppAPI/centerInfo/";
              postData(strUrl, params, null, deferred);
              return deferred.promise;
          },
          //提交意见反馈
          do_get_submitfeedback_req: function (params) {
              var deferred = $q.defer();
              var strUrl = "AppAPI/submitfeedback/";
              postData(strUrl, params, null, deferred);
              return deferred.promise;
          },
          //跟新客户拨打标记
          do_get_uptaskcustcallstate_req: function (params) {
              var deferred = $q.defer();
              var strUrl = "AppAPI/uptaskcust/";
              postData(strUrl, params, custlist, deferred);
              return deferred.promise;
          },
          //增加拨打记录
          do_get_addcelllog_req: function (params) {
              var deferred = $q.defer();
              var strUrl = "AppAPI/addcelllog/";
              postData(strUrl, params, custlist, deferred);
              return deferred.promise;
          }, 
          //获取报表首页
          do_get_StamainInfo_req: function (params) {
              var deferred = $q.defer();
              var strUrl = "AppAPI/stamaininfo/";
              postData(strUrl, params, User_Info, deferred);
              return deferred.promise;
          },
          //获取最新版本
          do_queryLatestVersion_req: function () {
              var deferred = $q.defer();
              var obj = get_queryLatestVersion_Data();
              var strUrl = "AppAPI/queryLatestVersion";
              postData(strUrl, obj, queryLatestVersion_resp, deferred);
              return deferred.promise;
          },
          //查询新版本
          query_newVersion_req: function () {
              var deferred = $q.defer();
              var obj = null;
              var strUrl = "http://api.fir.im/apps/latest/com.chinaunicom.jswsl?api_token=fdc22cc0a50689f72e2f284717d94034&type=ios";
              postRequest(strUrl, obj, deferred);
              return deferred.promise;
          },
          //获取蒲公英上最新版本
          get_pgyNewVersion_req: function () {
              var deferred = $q.defer();
              var obj = {
                  "_api_key": "441ad932c32607fd128017371112cef4",
                  "shortcut": "jswsla"
              };
              var strUrl = "http://www.pgyer.com/apiv1/app/getAppKeyByShortcut";
              postRequest(strUrl, obj, deferred);
              return deferred.promise;
          }
      };
      //数据请求：这里使用的是des加密算法，先将原始数据进行加密传递给服务器端。之后再将返回的数据进行解密操作
      function postData(strUrl, requestObj, responseObj, deferred) {
          console.log("请求的参数:"+angular.toJson(requestObj));
          var myUrl = SERVERURL + strUrl;
          console.log("AppItfService--请求链接：" + myUrl);
          $ionicLoading.show({
              //template: '拼命加载中，请耐心等候...'
              template: '<ion-spinner icon="ripple" class="spinner-assertive"></ion-spinner>'
          });
          var config = {
              timeout: 20000,
              headers: {'Content-Type': 'application/json; charset=UTF-8'},
              params: requestObj
          }

          $http.post(myUrl, {}, config)
            .success(function (data) {
                $ionicLoading.hide();
                console.log("服务器端返回数据"+angular.toJson(data));
                deferred.resolve(data);
            })
            .error(function (data) {
                $ionicLoading.hide();
                deferred.reject(data);
            });
      }
      //构造加密数据的封包
      function get_enData(params)
      {
          var obj={
              "reqParam":params
          }
          return obj;
      }

      //构造登录接口的参数
      function get_Login_Data(params) {
          var obj = {
              "staffId":params.staffId,
              "password":hex_md5(params.password),
              "randomNum": params.randomNum,
              "Loing_Key": params.Loing_Key
          }
          return obj;
      } 
      //版本更新
      function get_queryLatestVersion_Data(params) {
          var obj = {
              "os": "android",// 系统平台  //android   ios
          }
          return obj;
      }
  }]);
